package pong.business.controller;

import java.awt.event.KeyListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;

import pong.business.events.KeyboardController;
import pong.business.events.PlayerInputListener;
import pong.business.events.PongElementListener;
import pong.business.items.Ball;
import pong.business.items.Player;
import pong.ui.window.GamePanel;

public class GameController implements ControllerFacade, ElementsInteraction, PlayerInputListener {

	private static final int NB_THREADS = 3;
	
	//TODO implements this functionality.
	private boolean playerPositionReversed = false;
	
	private Thread[] gameThreads = new Thread[NB_THREADS];
	
	private Ball ball;
	
	private Player playerLeft;
	
	private Player playerRight;
	
	private PongElementListener listener;
	
	private KeyboardController keyboardController;

	public GameController(PongElementListener listener) throws FileNotFoundException, IOException {
		super();
		this.listener = listener;
	}

	public KeyListener getKeyboardController() {
		if(keyboardController == null) {
			keyboardController = new KeyboardController(this);
		}
		return keyboardController;
	}

	public String getPlayerLeftName() {
		return playerLeft.getName();
	}

	public int getPlayerLeftScore() {
		return playerLeft.getScore();
	}

	public String getPlayerRightName() {
		return playerRight.getName();
	}

	public int getPlayerRightScore() {
		return playerRight.getScore();
	}
	
	public void startNewGame() {
		
		// Interrupts previous threads if necessary.
		for(int index = 0; index < NB_THREADS; index ++) {
			if(gameThreads[index] != null && (!gameThreads[index].isInterrupted())) {
				gameThreads[index].interrupt();
			}
		}
		
		// Creates new element and associated threads.
		ball = new Ball();
		gameThreads[0] = new Thread(ball);
		
		playerLeft = new Player();
		gameThreads[1] = new Thread(playerLeft);
		
		playerRight = new Player();
		gameThreads[2] = new Thread(playerRight);
		
		// Starts the thread.
		for(int index = 0; index < NB_THREADS; index ++) {
			gameThreads[index].interrupt();
		}
	}

	public void pause() {
		ball.setPaused(!ball.isPaused());
		playerLeft.setPaused(!playerLeft.isPaused());
		playerRight.setPaused(!playerRight.isPaused());
	}

	public void player1Down() {
		if(playerPositionReversed) {
			playerRight.setBatPosition(playerRight.getBatPosition() - 1);
		} else {
			playerLeft.setBatPosition(playerLeft.getBatPosition() - 1);
		}
	}

	public void player1Up() {
		if(playerPositionReversed) {
			playerRight.setBatPosition(playerRight.getBatPosition() + 1);
		} else {
			playerLeft.setBatPosition(playerLeft.getBatPosition() + 1);
		}
	}

	public void player2Down() {
		if(playerPositionReversed) {
			playerLeft.setBatPosition(playerRight.getBatPosition() - 1);
		} else {
			playerRight.setBatPosition(playerLeft.getBatPosition() - 1);
		}
	}

	public void player2Up() {
		if(playerPositionReversed) {
			playerLeft.setBatPosition(playerRight.getBatPosition() + 1);
		} else {
			playerRight.setBatPosition(playerLeft.getBatPosition() + 1);
		}
	}

	public float getBallDirection() {
		//TODO
		return 0;
	}
}
